<?php
	require_once APPPATH . 'controllers/base_controllers/base_controller.php';

	class Cmb_admin extends Base_controller
	{		
		public function __construct()
		{
			parent::__construct();
			
			$this->load->model('values/member_registration_value');
			$this->load->model('values/cwr_registrant_member_homepage_value');
			$this->load->model('values/person_value');
			$this->load->model('values/member_info_value');
		}
				
		public function decode_passwords( $urlKey )
		{			
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->library('encrypt');
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_admin/true/' );
			}
			
			//$seckey = $this->Person_proxy->getPrimaryPersonSecurityKey( $urlKey );			
			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_home/true/' );
			}
			
			echo "Hello Admin Decode Passwords <br/>";
			die;

			$retval = $this->Admin_proxy->decodePersonPasswords();
			
			echo "Completed " . $retval . " <br/>";
			
			die;
		}
		
		public function encode_passwords( $urlKey )
		{
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->library('encrypt');
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_admin/true/' );
			}
			
			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_admin/true/' );
			}
/*			
			echo "Hello Admin Encode Passwords <br/>";
			die;
*/
			$retval = $this->Admin_proxy->encodePersonPasswords();
			
			echo "Completed " . $retval . " <br/>";
			
			die;
		}

		public function update_password_md5( $urlKey )
		{
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->library('encrypt');
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_admin/true/' );
			}
			
			//$seckey = $this->Person_proxy->getPrimaryPersonSecurityKey( $urlKey );			
			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_admin/true/' );
			}
			
			$retval = $this->Admin_proxy->encodePersonPasswordsMD5();
			
			echo "Completed " . $retval . " <br/>";
			
			die;
		}

		//Initial attendance setup for a new year
		public function setup_attendance( $urlKey )
		{
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Walk_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->library('encrypt');
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_admin/true/' );
			}
			
			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_admin/true/' );
			}
			
			$curyear = $this->Walk_proxy->getCurrentYear();
			
			$bvcount = $this->Admin_proxy->initializeBalaviharAttendance( $curyear );

			$langcount = $this->Admin_proxy->initializeLanguageAttendance( $curyear );

			echo "Processed: ".$bvcount." Balavihar Entries<br/>";
			echo "Processed: ".$langcount." Language Entries<br/>";
			
			die;
		}

		public function send_teacher_roster_emails( $urlKey )
		{			
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Walk_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->library('encrypt');
			
			$curyear = $this->Walk_proxy->getCurrentYear();
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_superadmin/true/' );
			}
		
			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_superadmin/true/' );
			}
			
			$retval = $this->Admin_proxy->sendTeacherRosterEmails($curyear);
			
			if ($retval > 0)
				echo "Sent " . $retval . " Emails<br/>";
			else
				echo "No Emails Sent<br/>";
			
			die;
		}
		
		public function send_registration_emails( $urlKey )
		{			
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Walk_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->library('encrypt');
			
			$curyear = $this->Walk_proxy->getCurrentYear();
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_superadmin/true/' );
			}
		
			//$seckey = $this->Person_proxy->getPrimaryPersonSecurityKey( $urlKey );			
			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_superadmin/true/' );
			}
			
			$retval = $this->Admin_proxy->sendRegistrationEmails($curyear);
			
			if ($retval > 0)
				echo "Sent " . $retval . " Emails<br/>";
			else
				echo "No Emails Sent<br/>";
			
			die;
		}
		
		//Send emails to people who have done step1 of registration, reminding to pay
		public function send_payment_emails( $urlKey )
		{			
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->model('proxies/Walk_proxy');
			$this->load->model('values/cwr_registrant_member_homepage_value');
			$this->load->model('proxies/Cwr_registrant_member_proxy');
			$this->load->library('encrypt');
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_superadmin/' . $urlKey . '/' );
			}

			//$seckey = $this->Person_proxy->getPrimaryPersonSecurityKey( $urlKey );			
			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_superadmin/true/' );
			}

			$curyear = $this->Walk_proxy->getCurrentYear();
			
			//Get registration id's for people who have not paid
			$regidarr = $retval = $this->Admin_proxy->getNotPaidID( $curyear );
			
			$numNotPaid = count($regidarr);
			if ($numNotPaid > 0)
			{
				for($i = 0; $i < $numNotPaid; $i++ )
				{				
					$memURLKey = $this->Walk_proxy->getMemberURLKey( $regidarr[$i] );
					$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($this->Cwr_registrant_member_proxy->getRegistrantMemberHomepageFromUrl( $memURLKey, $curyear));
					if (!$cwr_registrant_home_value->getIsRegistered())
					{
						redirect( base_url() . 'cmb/member/homepage_error/');
					}
								
					if (!$this->Walk_proxy->isRegistrationPaid( $regidarr[$i] ))
					{									
						//Update specific fields based on values that were entered by user
						if($cwr_registrant_home_value->getCMBCenter() == "Andover")
						{
							$cwr_registrant_home_value->setSession( $this->input->post('primaryAttendSatsang') );
						}
					
						$memberRegistrationValue = new Member_registration_value();
										
						$memberRegistrationValue->setCMBRegistrationFee( $cwr_registrant_home_value->getRegAmount() );
						$memberRegistrationValue->setBVRegistrationFee( $cwr_registrant_home_value->getBVAmount() );
						$memberRegistrationValue->setDonation( $cwr_registrant_home_value->getDonation() );
						$langFee = 0.0;
						$memberRegistrationValue->setLangRegistrationFee( $langFee );
						$totfee = $memberRegistrationValue->getTotalRegistrationFee();
						
						//Send reminder email
						$this->Admin_proxy->sendMemberPaymentReminderEmail( $cwr_registrant_home_value, $curyear, $totfee );
						//echo $regidarr[$i] . " , " . $person->getFirstName() . " , " . $person->getLastName() . "<br/>";		
						
						//Update status for payment email sent
						$this->Admin_proxy->setPaymentEmailSent( $regidarr[$i] );
						
						//Free memory
						unset($cwr_registrant_home_value);
					}
					else
					{
						echo "Registration for ".$cwr_registrant_home_value->getFirstName()." ".$cwr_registrant_home_value->getLastName()." is already paid <br/>";
					}
					
					sleep(1);
				} //Loop
				
				echo $numNotPaid . " payment reminder emails sent.";
			}
			else
			{
				echo "No Entries Found";
			}
			
			die;
		}		
		
		//Send emails to people who have to send in their payment check
		public function send_payment_check_emails( $urlKey )
		{			
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->model('proxies/Walk_proxy');
			$this->load->model('values/cwr_registrant_member_homepage_value');
			$this->load->model('proxies/Cwr_registrant_member_proxy');
			$this->load->library('encrypt');
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_superadmin/' . $urlKey . '/' );
			}

			//$seckey = $this->Person_proxy->getPrimaryPersonSecurityKey( $urlKey );			
			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_superadmin/true/' );
			}

			$curyear = $this->Walk_proxy->getCurrentYear();
			
			//Get registration id's for people who have not paid and have to send a check
			$regidarr = $retval = $this->Admin_proxy->getNotPaidByCheckID( $curyear );
			
			$numNotPaid = count($regidarr);
			if ($numNotPaid > 0)
			{
				for($i = 0; $i < $numNotPaid; $i++ )
				{				
					$memURLKey = $this->Walk_proxy->getMemberURLKey( $regidarr[$i] );
					$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($this->Cwr_registrant_member_proxy->getRegistrantMemberHomepageFromUrl( $memURLKey, $curyear));
					if (!$cwr_registrant_home_value->getIsRegistered())
					{
						redirect( base_url() . 'cmb/member/homepage_error/');
					}
								
					if (!$this->Walk_proxy->isRegistrationPaid( $regidarr[$i] ))
					{									
						//Update specific fields based on values that were entered by user
						if($cwr_registrant_home_value->getCMBCenter() == "Andover")
						{
							$cwr_registrant_home_value->setSession( $this->input->post('primaryAttendSatsang') );
						}
					
						$memberRegistrationValue = new Member_registration_value();
										
						$memberRegistrationValue->setCMBRegistrationFee( $cwr_registrant_home_value->getRegAmount() );
						$memberRegistrationValue->setBVRegistrationFee( $cwr_registrant_home_value->getBVAmount() );
						$memberRegistrationValue->setDonation( $cwr_registrant_home_value->getDonation() );
						$langFee = 0.0;
						$memberRegistrationValue->setLangRegistrationFee( $langFee );
						$totfee = $memberRegistrationValue->getTotalRegistrationFee();
						
						//Send reminder email to send check
						$this->Admin_proxy->sendMemberCheckReminderEmail( $cwr_registrant_home_value, $curyear, $totfee );
						//echo $regidarr[$i] . " , " . $person->getFirstName() . " , " . $person->getLastName() . "<br/>";		
						
						//Update status for payment email sent
						$this->Admin_proxy->setPaymentEmailSent( $regidarr[$i] );
						
						//Free memory
						unset($cwr_registrant_home_value);
					}
					else
					{
						echo "Registration for ".$cwr_registrant_home_value->getFirstName()." ".$cwr_registrant_home_value->getLastName()." is already paid <br/>";
					}
					
					sleep(1);
				} //Loop
				
				echo $numNotPaid . " payment reminder emails sent.";
			}
			else
			{
				echo "No Entries Found";
			}
			
			die;
		}		

		//Send confirmation emails to people who have registered and paid
		public function send_payment_confirmation_emails( $urlKey )
		{			
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->model('proxies/Walk_proxy');
			$this->load->model('values/cwr_registrant_member_homepage_value');
			$this->load->model('proxies/Cwr_registrant_member_proxy');
			$this->load->library('encrypt');
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_superadmin/' . $urlKey . '/' );
			}

			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_superadmin/true/' );
			}

			$curyear = $this->Walk_proxy->getCurrentYear();
			
			//Get registration id's for people who paid
			$regidarr = $retval = $this->Admin_proxy->getPaidID( $curyear );
			
			$numPaid = count($regidarr);
			if ($numPaid > 0)
			{
				for($i = 0; $i < $numPaid; $i++ )
				{				
					$memURLKey = $this->Walk_proxy->getMemberURLKey( $regidarr[$i] );
					$cwr_registrant_home_value = Cwr_registrant_member_homepage_value::cast($this->Cwr_registrant_member_proxy->getRegistrantMemberHomepageFromUrl( $memURLKey, $curyear));
					if (!$cwr_registrant_home_value->getIsRegistered())
					{
						redirect( base_url() . 'cmb/member/homepage_error/');
					}
								
					if ($this->Walk_proxy->isRegistrationPaid( $regidarr[$i] ))
					{									
						$memberRegistrationValue = new Member_registration_value();
						$person = new Person_value();
						
						$person->setUserName( $cwr_registrant_home_value->getUserName() );
						$person->setPassword( $cwr_registrant_home_value->getPassword() );
						$person->setSecretQuestion( $cwr_registrant_home_value->getSecretQuestion() );
						$person->setSecretPhrase( $cwr_registrant_home_value->getSecretPhrase() );
						$person->setFirstName( $cwr_registrant_home_value->getFirstName() );
						$person->setLastName( $cwr_registrant_home_value->getLastName() );
						$person->setGender( $cwr_registrant_home_value->getGender() );
						$person->setEmail1( $cwr_registrant_home_value->getEmail1() );
						$person->setPhone1( $cwr_registrant_home_value->getPhone1() );
						$person->setPhone2( $cwr_registrant_home_value->getPhone2() );
						$person->setOrgName( $cwr_registrant_home_value->getOrgName() );
						$person->setAddress1( $cwr_registrant_home_value->getAddress1() );
						$person->setAddress2( $cwr_registrant_home_value->getAddress2() );
						$person->setCity( $cwr_registrant_home_value->getCity() );
						$person->setState( $cwr_registrant_home_value->getState() );
						$person->setZip( $cwr_registrant_home_value->getZip() );
						
						$memberRegistrationValue->setPerson( $person );
						$memberRegistrationValue->setCurrentYear( $curyear );
										
						$memberRegistrationValue->setCMBRegHistoryId( $regidarr[$i] );
						$memberRegistrationValue->setCMBRegistrationFee( $cwr_registrant_home_value->getRegAmount() );
						$memberRegistrationValue->setBVRegistrationFee( $cwr_registrant_home_value->getBVAmount() );
						$memberRegistrationValue->setDonation( $cwr_registrant_home_value->getDonation() );
						$langFee = 0.0;
						$memberRegistrationValue->setLangRegistrationFee( $langFee );
						$totfee = $memberRegistrationValue->getTotalRegistrationFee();
						
						$paytype = $this->Walk_proxy->getCMBRegistrationPaymentType( $regidarr[$i] );
						
						//Send reminder email
						if ($paytype == "paypal")
						{
							$this->Walk_proxy->sendMemberRegistrationConfirmEmailPaypal( $memberRegistrationValue, $this->Walk_proxy->getCMBRegistrationPaymentPaypalID( $regidarr[$i] ) );
						}
						else
						{
							if ($paytype == "check")
							{
								$this->Walk_proxy->sendMemberRegistrationCheckConfirmEmail( $cwr_registrant_home_value, $curyear, $this->Walk_proxy->getCMBRegistrationPaymentCheckNum( $regidarr[$i] ), $memURLKey );
							}
							else
							{
								if ($this->Walk_proxy->getCMBRegistrationMemberType( $regidarr[$i] ) == "Life")
								{
									$this->Walk_proxy->sendMemberRegistrationConfirmEmailNoPay( $memberRegistrationValue );								
								}
							}
						}
						//Update status for reg confirm email sent
						$this->Admin_proxy->setRegConfirmEmailSent( $regidarr[$i] );
						
						//Free memory
						unset($cwr_registrant_home_value);
						unset($person);
					}
					else
					{
						echo "Registration for ".$cwr_registrant_home_value->getFirstName()." ".$cwr_registrant_home_value->getLastName()." is not paid <br/>";
					}
					
					sleep(1);
				} //Loop
				
				echo $numPaid . " payment confirmation emails sent.";
			}
			else
			{
				echo "No Entries Found";
			}
			
			die;
		}		

		//Send emails to register for CORD-Walk
		public function send_cwr_emails( $urlKey )
		{			
			$this->load->model('proxies/Admin_proxy');
			$this->load->model('proxies/Walk_proxy');
			$this->load->model('proxies/Person_proxy');
			$this->load->library('encrypt');
			
			$curyear = $this->Walk_proxy->getCurrentYear();
			
			$is_logged_in = $this->session->userdata( 'superadmin_is_logged_in' );
			if (!$is_logged_in)
			{
				redirect( base_url() . 'cmb/member/login_superadmin/true/' );
			}
		
			$seckey = $this->Person_proxy->getPersonSecurityKey( $this->session->userdata( 'person_id' ) );
			if ($seckey != $this->session->userdata( 'logged_in_key' ))
			{
				redirect( base_url() . 'cmb/member/login_superadmin/true/' );
			}
			
			$retval = $this->Admin_proxy->sendCWRRegistrationEmails($curyear);
			
			if ($retval > 0)
				echo "Sent " . $retval . " Emails<br/>";
			else
				echo "No Emails Sent<br/>";
			
			die;
		}
	}
?>